﻿using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Business
{
    public partial class Optinion : BaseUI.BasePage
    {
        private Sys_OptinionBLL optinionBLL = new Sys_OptinionBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageIng.CurrentPage = 1;
                PageIng_DataBinding(sender, e);

                //初始化GridView数据
                BindProjectName(false);
            }
        }

        #region 按钮事件

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            PageIng.CurrentPage = 1;
            PageIng_DataBinding(sender, e);
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_INSERT();");
            //Response.Redirect("OptinionEdit.aspx");
        }

        protected void BtnEdit_Click(object sender, EventArgs e)
        {
            int count = 0;
            for (int i = 0; i < grvShow.Rows.Count; i++)
            {
                if (((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    count++;
                    if (count == 1)
                    {
                        string pid = ((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).ToolTip; //((HiddenField)grvShow.Rows[i].FindControl("HiddID")).Value.ToString();
                        MessageBox.ResponseScript(this, "FORM_EDIT(\"" + pid + "\");");
                        return;
                    }
                }
            }
        }

        protected void btnDelele_Click(object sender, EventArgs e)
        {
            //string ids = "";
            List<decimal> ids = new List<decimal>();
            for (int i = 0; i < grvShow.Rows.Count; i++)
            {
                if (((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    string id = ((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).ToolTip;
                    ids.Add(decimal.Parse(id));
                    //ids += id + ",";
                }
            }

            if (ids.Count == 0)
            {
                MessageBox.Show(Page, "请选择要删除的逻辑条件");
                return;
            }
            else
            {
                if (optinionBLL.DeleteRange(ids, CurrentUserName) > 0)
                {
                    MessageBox.Show(Page, "删除成功");
                    PageIng.CurrentPage = 1;
                    PageIng_DataBinding(sender, e);
                    return;
                }
            }
        }

        #endregion 按钮事件

        #region 分页相关

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData((PageIng.CurrentPage - 1) * PageIng.RowsPerPage, PageIng.RowsPerPage);//PageIng.CurrentPage*PageIng.RowsPerPage
        }

        #endregion 分页相关

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData(int skip, int take)
        {
            int count;
            string sql = "";
            if (txtKey.Text != "")
            {
                sql += " and c_opin_name like '%" + txtKey.Text + "%'";
            }
            var dtItem = optinionBLL.GetPage(CurrentUserID, skip, take, sql, out count);
            if (dtItem != null && dtItem.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = count;
                PageIng.DataSource = dtItem;
                PageIng.GridViewSource = grvShow;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dtItem;
                PageIng.GridViewSource = grvShow;
                grvShow.DataSource = null;
                grvShow.DataBind();
                PageIng.Visible = false;
            }
        }

        /// <summary>
        /// GridView标题排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            BindProjectName(true);
        }

        /// <summary>
        /// 绑定项目名称（非动态）绑定GridView
        /// </summary>
        public void BindProjectName(bool iSsort)
        {
            DataTable dtItem = optinionBLL.GetSelect(CurrentUserID);

            DataView view = dtItem.DefaultView;
            if (iSsort)
            {
                string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
                view.Sort = sort;//设置排序
            }

            grvShow.DataSource = view;

            grvShow.DataBind();
        }
    }
}